//
//  ViewController.m
//  DanMu
//
//  Created by tgs on 2017/2/20.
//  Copyright © 2017年 tgs. All rights reserved.
//

#import "ViewController.h"
#import <BarrageRenderer/BarrageRenderer.h>
#import "BarrageSpriteProtocol.h"
#import "NSSafeObject.h"
#import "UIImage+Barrage.h"
#import "BarrageWalkImageTextSprite.h"

@interface ViewController ()<UITextFieldDelegate>{
    NSTimer * _timer;
    NSInteger _index;
}

@property (nonatomic, strong) UITextField *textInput;

@property (nonatomic, strong) BarrageRenderer *barrageRenderer;

@property (nonatomic, strong) UIView *viewDan;

@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _index = 0;
    self.arr = [[NSMutableArray alloc] initWithObjects:@"一一",@"二二",@"三三",@"四四",@"五五",@"六六",@"七七",@"八八",@"九九",@"十十",@"十一十一",@"十二十二十二",@"十三十三十三十三",@"十四十四十四",@"食物食物食物食物",@"十六十六十六十六是河流",@"时期时期时期时期起",@"十八十八十八十八十八十八",@"十九施救十九施救",@"而是二十二是二世二是",@"十十一二十一二十一",@"二十二二十二二十是二十二",@"二十三二十三二十三",@"二十四二十四二十五", nil];
    
    
    self.viewDan = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 400)];
    self.viewDan.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.viewDan];
    
    self.barrageRenderer = [[BarrageRenderer alloc] init];
    [self.viewDan addSubview:self.barrageRenderer.view];
    [self.viewDan sendSubviewToBack:self.barrageRenderer.view];
//    self.barrageRenderer.canvasMargin = UIEdgeInsetsMake(10, 10, 10, 10);
    self.barrageRenderer.view.userInteractionEnabled = YES;
    
    [self.barrageRenderer start];
    
    self.textInput = [[UITextField alloc] initWithFrame:CGRectMake(0, 420, 300, 25)];
    self.textInput.delegate = self;
    self.textInput.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:self.textInput];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSSafeObject * safeObj = [[NSSafeObject alloc]initWithObject:self withSelector:@selector(autoSendBarrage)];
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:safeObj selector:@selector(excute) userInfo:nil repeats:YES];
}

- (void)autoSendBarrage
{
    NSInteger spriteNumber = [self.barrageRenderer spritesNumberWithName:nil];
    self.infoLabel.text = [NSString stringWithFormat:@"哈哈哈哈: %ld",(long)spriteNumber];
    if (spriteNumber <= 500) { // 用来演示如何限制屏幕上的弹幕量
        [self.barrageRenderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionR2L side:BarrageWalkSideRight]];
        
//        [self.barrageRenderer receive:[self walkImageTextSpriteDescriptorAWithDirection:BarrageWalkDirectionR2L]];
        
//        [self.barrageRenderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionR2L side:BarrageWalkSideDefault]];
        
//        [self.barrageRenderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionB2T side:BarrageWalkSideLeft]];
//        [self.barrageRenderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionB2T side:BarrageWalkSideRight]];
//        
//        [self.barrageRenderer receive:[self floatTextSpriteDescriptorWithDirection:BarrageFloatDirectionB2T side:BarrageFloatSideCenter]];
//        [self.barrageRenderer receive:[self floatTextSpriteDescriptorWithDirection:BarrageFloatDirectionT2B side:BarrageFloatSideLeft]];
//        [self.barrageRenderer receive:[self floatTextSpriteDescriptorWithDirection:BarrageFloatDirectionT2B side:BarrageFloatSideRight]];
//
//        [self.barrageRenderer receive:[self walkImageSpriteDescriptorWithDirection:BarrageWalkDirectionR2L]];
//        [self.barrageRenderer receive:[self walkImageSpriteDescriptorWithDirection:BarrageWalkDirectionL2R]];
//        [self.barrageRenderer receive:[self floatImageSpriteDescriptorWithDirection:BarrageFloatDirectionT2B]];
    }
}

#pragma mark - 弹幕描述符生产方法

/// 生成精灵描述 - 过场文字弹幕
- (BarrageDescriptor *)walkTextSpriteDescriptorWithDirection:(BarrageWalkDirection)direction
{
    return [self walkTextSpriteDescriptorWithDirection:direction side:BarrageWalkSideDefault];
}

/// 生成精灵描述 - 过场文字弹幕
- (BarrageDescriptor *)walkTextSpriteDescriptorWithDirection:(BarrageWalkDirection)direction side:(BarrageWalkSide)side
{
//    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
//    descriptor.spriteName = NSStringFromClass([BarrageWalkTextSprite class]);
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageWalkImageTextSprite class]);
    
    if (_index < self.arr.count) {
//        NSLog(@"数字  %ld",_index);
        descriptor.params[@"text"] = [NSString stringWithFormat:@"%@",self.arr[_index]];
        descriptor.params[@"textColor"] = [UIColor blueColor];
        NSString *text = self.arr[_index];
        descriptor.params[@"speed"] = @(text.length * (double)random()/RAND_MAX+50);
        descriptor.params[@"direction"] = @(direction);
        descriptor.params[@"side"] = @(side);
        descriptor.params[@"delay"] = @(0);
        descriptor.params[@"clickAction"] = ^{
//                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"弹幕被点击" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//                    [alertView show];
        };
        _index++;
    }
    else {
        _index = 0;
    }
    
    return descriptor;
}

/// 生成精灵描述 - 浮动文字弹幕
- (BarrageDescriptor *)floatTextSpriteDescriptorWithDirection:(NSInteger)direction
{
    return [self floatTextSpriteDescriptorWithDirection:direction side:BarrageFloatSideCenter];
}

/// 生成精灵描述 - 浮动文字弹幕
- (BarrageDescriptor *)floatTextSpriteDescriptorWithDirection:(NSInteger)direction side:(BarrageFloatSide)side
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageFloatTextSprite class]);
    descriptor.params[@"text"] = [NSString stringWithFormat:@"悬浮文字弹幕:%ld",(long)_index++];
    descriptor.params[@"textColor"] = [UIColor purpleColor];
    descriptor.params[@"duration"] = @(3);
    descriptor.params[@"fadeInTime"] = @(1);
    descriptor.params[@"fadeOutTime"] = @(1);
    descriptor.params[@"direction"] = @(direction);
    descriptor.params[@"side"] = @(side);
    return descriptor;
}

/// 生成精灵描述 - 过场图片弹幕
- (BarrageDescriptor *)walkImageSpriteDescriptorWithDirection:(NSInteger)direction
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageWalkImageSprite class]);
    descriptor.params[@"image"] = [[UIImage imageNamed:@"avatar"]barrageImageScaleToSize:CGSizeMake(20.0f, 20.0f)];
    descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50);
    descriptor.params[@"direction"] = @(direction);
    descriptor.params[@"trackNumber"] = @5; // 轨道数量
    
    
    return descriptor;
}

/// 生成精灵描述 - 浮动图片弹幕
- (BarrageDescriptor *)floatImageSpriteDescriptorWithDirection:(NSInteger)direction
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageFloatImageSprite class]);
    descriptor.params[@"image"] = [[UIImage imageNamed:@"avatar"]barrageImageScaleToSize:CGSizeMake(40.0f, 15.0f)];
    descriptor.params[@"duration"] = @(3);
    descriptor.params[@"direction"] = @(direction);
    return descriptor;
}

/// 图文混排精灵弹幕 - 过场图文弹幕A
- (BarrageDescriptor *)walkImageTextSpriteDescriptorAWithDirection:(NSInteger)direction
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageWalkImageTextSprite class]);
//    descriptor.params[@"text"] = [NSString stringWithFormat:@"AA-图文混排/::B过场弹幕:%ld",(long)_index++];
//    descriptor.params[@"textColor"] = [UIColor greenColor];
//    descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50);
//    descriptor.params[@"direction"] = @(direction);
    
    if (_index < self.arr.count) {
        //        NSLog(@"数字  %ld",_index);
        descriptor.params[@"text"] = [NSString stringWithFormat:@"%@",self.arr[_index]];
        descriptor.params[@"textColor"] = [UIColor blueColor];
        NSString *text = self.arr[_index];
        descriptor.params[@"speed"] = @(text.length * (double)random()/RAND_MAX+50);
        descriptor.params[@"direction"] = @(direction);
//        descriptor.params[@"side"] = @(side);
        descriptor.params[@"delay"] = @(0);
        descriptor.params[@"clickAction"] = ^{
            //                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"弹幕被点击" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            //                    [alertView show];
        };
        _index++;
    }
    else {
        _index = 0;
    }
    
    return descriptor;
}

#pragma mark - Action
- (IBAction)start:(id)sender
{
    [self.barrageRenderer start];
}
- (IBAction)stop:(id)sender
{
    [self.barrageRenderer stop];
}
- (IBAction)pause:(id)sender
{
    [self.barrageRenderer pause];
}
- (IBAction)resume:(id)sender
{
    [self.barrageRenderer start];
}

- (IBAction)faster:(id)sender
{
    CGFloat speed = self.barrageRenderer.speed + 0.5;
    if (speed >= 10) {
        speed = 10.0f;
    }
    self.barrageRenderer.speed = speed;
}
- (IBAction)slower:(id)sender
{
    CGFloat speed = self.barrageRenderer.speed - 0.5;
    if (speed <= 0.5f) {
        speed = 0.5;
    }
    self.barrageRenderer.speed = speed;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
//    [self.arr addObject:textField.text];
    [self.arr insertObject:textField.text atIndex:_index];
    
      [self.barrageRenderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionR2L side:BarrageWalkSideRight]];
}

@end
